package com.A1908.dao.impl;

import com.A1908.dao.MyAttendDao;
import com.A1908.entity.MyAttend;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MyAttendDaoImpl extends BaseDao implements MyAttendDao {
    @Override
    public List<MyAttend> getAllMyAttend(MyAttend myAttend) {
        Connection connection=conn();
        String sql="\n" +
                "SELECT\n" +
                "\tmyattend.*,\n" +
                "\trelationtype.relationType \n" +
                "FROM\n" +
                "\tmyattend\n" +
                "\tINNER JOIN relationtype ON myattend.hostRealtion = relationtype.id\n" +
                "\tWHERE myUserID=?;";
        Object [] objs={myAttend.getMyUserID()};
        PreparedStatement preparedStatement=super.pstmt(connection,sql,objs);
        ResultSet rs=null;
        try {
            rs = preparedStatement.executeQuery();
            List<MyAttend> list=new ArrayList<>();
            while (rs.next()){
                MyAttend ma=new MyAttend();
                ma.setId(rs.getInt(1));
                ma.setMyUserID(rs.getInt(2));
                ma.setMyAttendName(rs.getString(3));
                ma.setMyAttendAddress(rs.getString(4));
                ma.setCostMoney(rs.getDouble(5));
                ma.setHostRealtion(rs.getInt(6));
                ma.setHostPeople(rs.getString(7));
                ma.setHostRealtionName(rs.getString(8));
                list.add(ma);
            }
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int addMyAttend(MyAttend myAttend) {
        String sql="INSERT INTO myattend VALUES(DEFAULT,?,?,?,?,?,?);";
        Object [] objs={myAttend.getMyUserID(),myAttend.getMyAttendName(),myAttend.getMyAttendAddress(),myAttend.getCostMoney(),myAttend.getHostRealtion(),myAttend.getHostPeople()};
        return executeUpdate(sql,objs);
    }

    @Override
    public int editMyAttend(MyAttend myAttend) {
        String sql="UPDATE myattend SET myAttendfestName=?,myAttendfestAddress=?,costMoney=?,hostRealtion=?,hostPeople=? WHERE myUserID=?;";
        Object [] objs={myAttend.getMyAttendName(),myAttend.getMyAttendAddress(),myAttend.getCostMoney(),myAttend.getHostRealtion(),myAttend.getHostPeople(),myAttend.getMyUserID()};
        return executeUpdate(sql,objs);
    }

    @Override
    public int delMyAttend(MyAttend myAttend) {
        return 0;
    }
}
